package com.hydl.power.controller;

import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.mapper.GatewayMapper;
import com.hydl.power.mapper.VoltageMapper;
import com.hydl.power.mapper.WaterInvasionMapper;
import com.hydl.power.pojo.*;
import com.hydl.power.service.DeviceService;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "DeviceController", tags = "设备控制类")
@RestController
@RequestMapping("/device")
@CrossOrigin
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private GatewayMapper gatewayMapper;

    @Autowired
    private WaterInvasionMapper waterInvasionMapper;

    @Autowired
    private VoltageMapper voltageMapper;

 /*   @ApiOperation("根据项目id查询设备列表")
    @PostMapping("/getAllDevice")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",  dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目ID",required = true,defaultValue = "50642475", dataType = "String")
    })
    public Resp<PageInfo<Device>> selectDevice(Integer pageNum, Integer pageSize, String projectId) {
        PageInfo<Device> list = deviceService.selectDevice(pageNum, pageSize, projectId);
        return Resp.success(list);
    }*/

    /*@ApiOperation("根据单位项目id查询配电房列表")
    @PostMapping("/getPowerRoomByNavId")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "navId", value = "单位项目id",required = true,defaultValue = "07938132",  dataType = "String"),
    })
    public Resp<List<PowerRoom>> queryByNavId(String navId) {
        return Resp.success(deviceService.queryByNavId(navId));
    }*/

/*    @ApiOperation("查询所有设备列表")
    @PostMapping("/getAll")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",  dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", dataType = "Integer")
    })
    public Resp<PageInfo<Device>> getAll(Integer pageNum, Integer pageSize) {
        PageInfo<Device> list = deviceService.getAll(pageNum, pageSize);
        return Resp.success(list);
    }*/


    // @LogAnnotation(module = "设备控制类",description = "根据条件查询设备")
    @ApiOperation("根据条件查询设备")
    @PostMapping("/queryByCondition")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",  dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "gatewaySn", value = "网关sn", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "toekn", dataType = "String"),
    })
    public Resp<PageInfo<Device>> queryByCondition(@RequestParam(required = false )Integer pageNum,
                                                   @RequestParam(required = false )Integer pageSize,
                                                   @RequestParam(required = false) String cityId,
                                                   @RequestParam(required = false) String projectId,
                                                   @RequestParam(required = false) String roomId,
                                                   @RequestParam(required = false) String gatewaySn,
                                                   @RequestParam(required = false) String token) {
         PageInfo<Device> list = deviceService.queryByCondition(pageNum, pageSize, cityId, projectId, roomId, gatewaySn,token);
        return Resp.success(list);
    }


    @LogAnnotation(module = "设备控制类",description = "新增设备")
    @PostMapping("/addDevice")
    @ApiOperation(value = "新增设备", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceName", value = "设备名字", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "地区id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceAddress", value = "设备地址", required = true,defaultValue = "00521229", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "serialNumber", value = "设备编码", required = true,defaultValue = "66666666", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "gatewaySn", value = "网关sn",required = false,defaultValue = "07938132",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceMessageAddress", value = "设备通讯地址",required = false,defaultValue = "07938132",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "commonalityDdress", value = "公共地址",required = false,defaultValue = "07938132",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "aisle", value = "通道",required = false,defaultValue = "07938132",dataType = "String"),
    })
    public Resp addDevice(String deviceName,String areaId,String projectId,String powerRoomId,String serialNumber,
                          String gatewaySn) {

        Device device = deviceService.selectAllBySn(gatewaySn);
        if (device != null){
            return Resp.fail("设备重复");
        }
/*        Device device = deviceService.selectByserialNumber(serialNumber);
        if (device != null){
            return Resp.fail("设备序列号重复");
        }*/
        int i = deviceService.addDevice(deviceName, areaId, projectId, powerRoomId,serialNumber,gatewaySn);
        if (i == 1){
            return Resp.success("Success");
        }
        return Resp.fail("添加失败");
    }


    @PostMapping("/deleteDeviceById")
    @ApiOperation(value = "删除设备", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "设备id",  dataType = "Integer"),
    })
    public Resp deleteDeviceById(Integer id) {
        int i = deviceService.deleteNaviceById(id);
        if (i == 1){
            return Resp.success("删除成功");
        }
        return Resp.success("删除失败");
    }


    // @LogAnnotation(module = "设备控制类",description = "根据ID获取设备")
    @ApiOperation(value="根据ID获取设备")
    @PostMapping("/getDeviceById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "设备id", dataType = "Integer"),
    })
    public Resp getDeviceById(Integer id){
        return Resp.success(deviceService.getDeviceById(id));
    }


    @LogAnnotation(module = "设备控制类",description = "修改设备")
    @PostMapping("/updateDeviceById")
    @ApiOperation(value = "修改设备", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "设备id",  dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "deviceName", value = "设备名字", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceAddress", value = "设备地址", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceMessageAddress", value = "设备通讯地址", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "commonalityDdress", value = "公共地址", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "serialNumber", value = "设备编号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "aisle", value = "通道",dataType = "Integer")
    })
    public Resp updateDeviceById(String deviceName,String deviceAddress,String deviceMessageAddress,String commonalityDdress,String aisle,Integer id,String serialNumber) {
/*        Device device = deviceService.selectByserialNumber(serialNumber);
       if (device != null){
            return Resp.fail("设备序列号重复");
        }*/
        int i = deviceService.updateDeviceById(deviceName,deviceAddress, deviceMessageAddress, commonalityDdress,aisle,id,serialNumber);
        if (i == 1){
            return Resp.success("修改成功");
        }
        return Resp.success("修改失败");
    }


    //  @LogAnnotation(module = "设备控制类",description = "根据配电房id查询网关")
    @ApiOperation("根据配电房id查询网关")
    @PostMapping("/queryGatewayByPowerId")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房Id",  dataType = "String")
    })
    public Resp queryGatewayByPowerId( String roomId) {

        return Resp.success(gatewayMapper.queryGatewayByPowerId(roomId));
    }

    // @LogAnnotation(module = "根据配电房id查询进线柜",description = "根据配电房id查询进线柜")
    @ApiOperation("根据配电房id查询进线柜")
    @PostMapping("/queryByDeviceIdLike")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id",  dataType = "String")
    })
    public Resp queryByDeviceIdLike( String roomId) {
      List<Device> list=deviceService.queryByDeviceIdLike(roomId);
      if(list.size()==0){
        return Resp.success("没有查询到数据",null,null);
      }
      return Resp.success(list);
    }

    @ApiOperation("根据token查询设备")
    @PostMapping("/queryDeviceByProjectId")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "配电房id",  dataType = "String")
    })
    public Resp getDeviceByProjectId( String token) {
        List<Device> list=deviceService.getDeviceByProjectId(token);
        if(list==null){
            return Resp.success("未查询到数据",list,null);
        }
        if(list.size() == 0){
            return Resp.success("未查询到数据",list,null);
        }
        return Resp.success(list);
    }

    @ApiOperation("根据配电房id查询水浸设备")
    @PostMapping("/queryWaterInvasionRoomId")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",  dataType = "String")
    })
    public Resp queryWaterInvasionRoomId( String powerRoomId) {
        List<WaterInvasion> list=waterInvasionMapper.queryWaterInvasionList(powerRoomId);
        if(list!=null){
            for (WaterInvasion waterInvasion : list) {
                waterInvasion.setStatus(0);
            }
        }else {
            return Resp.success("未查询到数据",list,null);
        }
        if(list.size() == 0){
            return Resp.success("未查询到数据",list,null);
        }
        return Resp.success(list);
    }


    @ApiOperation("根据配电房id查询电压设备")
    @PostMapping("/queryVoltage")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",  dataType = "String")
    })
    public Resp queryVoltage( String powerRoomId){
        List<Voltage> list=voltageMapper.queryVoltageList(powerRoomId);
        if(list == null){
            return Resp.success("未查询到数据",list,null);
        }
        if(list.size() == 0){
            return Resp.success("未查询到数据",list,null);
        }
        return Resp.success(list);
    }

    @ApiOperation("配电房id查询设配序列号")
    @PostMapping("/queryFacility")
    public Resp<List> queryFacility(){
        return Resp.success(deviceService.queryFacility());
    }
}
